import {isNumber} from "@/utils/is"

export const ByteRange_Pro = (rule,value,callback,size)=>{
    let len=0
    for(let i=0;i<value.length;++i){
        const c = value.charCodeAt(i)
        if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
            len += 1
        } else {
            len += 2
        }
    }
    if(size < len){
        return Promise.reject(rule.message)
    }
    return Promise.resolve()
}

export const IsIP_Pro = (rule,value,callback)=>{
    const strs=value?.split(".")
    if(4!=strs?.length){
        return Promise.reject("请按照127.0.0.1的格式输入ip")
    }
    let OK=true
    Object.values(strs).forEach((str: string)=>{
        const num=parseInt(str)
        if(isNaN(num)||0>num||255<num){
            OK=false
        }
    })
    if(!OK){
        return Promise.reject("ip的范围为0.0.0.0-255.255.255.255")
    }
    return Promise.resolve()
}

export const IsIP = (value)=>{
    const strs=value?.split(".")
    if(4!=strs?.length){
        return false
    }
    let OK=true
    Object.values(strs).forEach((str: string)=>{
        const num=parseInt(str)
        if(isNaN(num)||0>num||255<num){
            OK=false
        }
    })
    return OK
}

export const IPCompareLower=(IP1,IP2)=>{
    const strs1=IP1?.split(".")
    const strs2=IP2?.split(".")
    if(4!=strs2?.length||4!=strs1?.length){
        return false
    }
    for(let i=0;i<4;++i){
        if(Number(strs1[i])<Number(strs2[i])){
            return true
        }
    }
    return false
}
